<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="玩家号" prop="userInviteCode">
              <el-input v-model="queryParams.userInviteCode" placeholder="请输入玩家号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">

      <el-table v-loading="loading" :data="userShopList" @selection-change="handleSelectionChange">
        <el-table-column label="店铺头像" align="center" prop="shopHeadPortrait">
          <template #default="scope">
            <el-image style="width: 100px; height: 100px" :src="scope.row.shopHeadPortrait" fit="fill" />
          </template>
        </el-table-column>
        <el-table-column label="店铺名" align="center" prop="shopName" />
        <el-table-column label="用户头像" align="center" prop="userHeadPortrait">
          <template #default="scope">
            <el-image style="width: 100px; height: 100px" :src="scope.row.userHeadPortrait" fit="fill" />
          </template>
        </el-table-column>
        <el-table-column label="用户昵称" align="center" prop="nickName" />
        <el-table-column label="玩家号" align="center" prop="userInviteCode" />
        <el-table-column label="累计消费金额" align="center" prop="sumPayMoney" />
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="UserShop" lang="ts">
import { listUserShop, getUserShop, delUserShop, addUserShop, updateUserShop } from '@/api/system/userConsum';
import { UserShopVO, UserShopQuery, UserShopForm } from '@/api/system/userConsum/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const userShopList = ref<UserShopVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const userShopFormRef = ref<ElFormInstance>();

const initFormData: UserShopForm = {
  id: undefined,
  userInviteCode: undefined,
  appUserId: undefined,
  shopId: undefined,
  sumPayMoney: undefined
}
const data = reactive<PageData<UserShopForm, UserShopQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userInviteCode: undefined,
    shopName: undefined,
    appUserId: undefined,
    shopId: undefined,
    sumPayMoney: undefined,
    params: {
    }
  },
  rules: {
    id: [
      { required: true, message: "主键不能为空", trigger: "blur" }
    ],
    userInviteCode: [
      { required: true, message: "玩家号不能为空", trigger: "blur" }
    ],
    appUserId: [
      { required: true, message: "用户主键不能为空", trigger: "blur" }
    ],
    shopId: [
      { required: true, message: "店铺id不能为空", trigger: "blur" }
    ],
    sumPayMoney: [
      { required: true, message: "累计消费金额不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询每个用户在店铺下得消费累计实付金额列表 */
const getList = async () => {
  loading.value = true;
  const res = await listUserShop(queryParams.value);
  userShopList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  userShopFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: UserShopVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 提交按钮 */
const submitForm = () => {
  userShopFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateUserShop(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addUserShop(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

onMounted(() => {
  getList();
});
</script>
